<?php

/**
 * Answer form.
 *
 * @package    tests
 * @subpackage form
 * @author     Djul
 */
class TeststartForm extends BaseForm
{
  public function configure()
  {
      $this->widgetSchema['subject'] = new sfWidgetFormPropelChoice(array('model' => 'Subject', 'add_empty' => 'виберіть предмет'));
      $this->validatorSchema['subject'] = new sfValidatorPropelChoice(array('model' => 'Subject', 'required' => true));

      $c = new Criteria();
      $id = $this->getOption('id');
      
      if($id != '')
      {
          $c = new Criteria();
          $c->add(TestBlockPeer::SUBJECT_ID, $id);
      }
      $this->widgetSchema['test_block'] = new sfWidgetFormPropelChoice(array('model' => 'TestBlock', 'add_empty' => 'виберіть блок тестів', 'criteria' => $c, 'method' => 'getTestName'));
      $this->validatorSchema['test_block'] = new sfValidatorPropelChoice(array('model' => 'TestBlock', 'column' => 'id', 'required' => true));

      $c = new Criteria();
      $block_id = $this->getOption('block_id');
      if($block_id != '')
      {
          $c->addJoin(QuestionPeer::TEST_ID, TestPeer::ID);
          $c->addJoin(TestPeer::TEST_BLOCK_ID, $block_id);
          $c->add(QuestionPeer::LEVEL, 1);
          $task1 = QuestionPeer::doCount($c);

          $c = new Criteria();
          $c->addJoin(QuestionPeer::TEST_ID, TestPeer::ID);
          $c->addJoin(TestPeer::TEST_BLOCK_ID, $block_id);
          $c->add(QuestionPeer::LEVEL, 2);
          $task2 = QuestionPeer::doCount($c);

          $c = new Criteria();
          $c->addJoin(QuestionPeer::TEST_ID, TestPeer::ID);
          $c->addJoin(TestPeer::TEST_BLOCK_ID, $block_id);
          $c->add(QuestionPeer::LEVEL, 3);
          $task3 = QuestionPeer::doCount($c);

          $c = new Criteria();
          $c->addJoin(TestPeer::TEST_BLOCK_ID, $block_id);
          $c->add(TestPeer::IS_PUBLISHED, true);
          $c->add(TestPeer::AMOUNT_TASK_LEVEL1, $task1, Criteria::LESS_EQUAL);
          $c->add(TestPeer::AMOUNT_TASK_LEVEL2, $task2, Criteria::LESS_EQUAL);
          $c->add(TestPeer::AMOUNT_TASK_LEVEL3, $task3, Criteria::LESS_EQUAL);
      }
      
      $this->widgetSchema['test'] = new sfWidgetFormPropelChoice(array('model' => 'Test', 'add_empty' => 'виберіть тест', 'criteria' => $c));
      $this->validatorSchema['test'] = new sfValidatorPropelChoice(array('model' => 'Test', 'column' => 'id', 'required' => true));

      $this->widgetSchema->setNameFormat('Teststart[%s]');
    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

}